﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
using System.ComponentModel;

namespace ResourceBlender_Express {
    /// <summary>
    /// A window for using Google Translate service to translate an application
    /// </summary>
    public partial class WinAutoTranslate : Window {
        private bool success = false;
        /// <summary>
        /// Gets whether the translation was completed successfully.
        /// </summary>
        /// <value><c>true</c> if successful; otherwise, <c>false</c>.</value>
        public bool Success { get { return success; } }

        /// <summary>
        /// Initializes a new <see cref="WinAutoTranslate"/>.
        /// </summary>
        public WinAutoTranslate() {
            InitializeComponent();
            lstSourceType.ItemsSource = GoogleTranslator.GoogleSupportedLanguages;
            lstDestinationType.ItemsSource = GoogleTranslator.GoogleSupportedLanguages;
            appSelector.Mode = ApplicationSelector.SelectionMode.SingleApplicationBundles;
            appSelector.IsBusy += (busy) => {
                if(busy) {
                    SetStatus("Loading applications...", true);
                } else {
                    SetStatus(null, false);
                }
            };
        }

        /// <summary>
        /// Selects the default language of a new application when the selected values in the application selector change.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void AppSelector_PropertyChanged(object sender, PropertyChangedEventArgs e) {
            if(appSelector.SelectedApplications.Count == 0) return;
            lstSourceLanguage.SelectedValue = appSelector.SelectedApplications[0].DefaultLanguage;
            if(lstSourceLanguage.SelectedItem != null) {
                lstSourceLanguage.ScrollIntoView(lstSourceLanguage.SelectedItem);
            }
        }

        /// <summary>
        /// Selects the right Google code when the selected source or destination language changes.
        /// The first time this happens (and every time a new application is selected whose default language was not a language in the previously selected application),
        /// the event fires before the ItemsControl has time to swap over the items, resulting in a null SelectedValue.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void LanguageSelected(object sender, SelectionChangedEventArgs e) {
            if(sender == lstSourceLanguage && lstSourceLanguage.SelectedValue != null) {
                lstSourceType.SelectedValue = GoogleTranslator.DotNetCode2GoogleCode(lstSourceLanguage.SelectedValue.ToString());
                if(lstSourceType.SelectedValue != null) {
                    lstSourceType.ScrollIntoView(lstSourceType.SelectedItem);
                }
            } else if(sender == lstDestinationLanguage && lstDestinationLanguage.SelectedValue != null) {
                lstDestinationType.SelectedValue = GoogleTranslator.DotNetCode2GoogleCode(lstDestinationLanguage.SelectedValue.ToString());
                if(lstDestinationType.SelectedValue != null) {
                    lstDestinationType.ScrollIntoView(lstDestinationType.SelectedItem);
                }
            }
        }

        /// <summary>
        /// Starts automatic translation.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AutoTranslate(object sender, RoutedEventArgs e) {
            if(ValidateSelection()) {
                WinTranslator dlg = new WinTranslator(lstSourceLanguage.SelectedValue.ToString(), 
                                                      lstSourceType.SelectedValue.ToString(), 
                                                      lstDestinationLanguage.SelectedValue.ToString(), 
                                                      lstDestinationType.SelectedValue.ToString(),
                                                      new List<RBApplication>() {appSelector.SelectedApplications[0]},
                                                      appSelector.SelectedBundles,
                                                      radOverwrite.IsChecked == true ? true : false,
                                                      chkVerbose.IsChecked == true ? true : false);
                dlg.Owner = this;
                dlg.ShowDialog();
                if(dlg.Success == true) {
                    this.success = true;
                    Close();
                }
            }
        }

        /// <summary>
        /// Validates the selection is valid.
        /// </summary>
        /// <returns></returns>
        private bool ValidateSelection() {
            bool valid = true;
            if(!appSelector.ValidateSelection(true)) {
                valid = false;
            } else if(lstSourceLanguage.SelectedValue == null || lstSourceType.SelectedValue == null) {
                DialogBox.ShowAlert(this, "Source language and type must be specified", "Error");
                valid = false;
            } else if(lstDestinationLanguage.SelectedValue == null || lstDestinationType.SelectedValue == null) {
                DialogBox.ShowAlert(this, "Destination language and type must be specified", "Error");
                valid = false;
            } else if(lstSourceLanguage.SelectedValue == lstDestinationLanguage.SelectedValue || lstSourceType.SelectedValue == lstDestinationType.SelectedValue) {
                DialogBox.ShowAlert(this, "Source and destination languages must be different", "Error");
                valid = false;
            }
            return valid;
        }

        /// <summary>
        /// Shows the help file.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void ShowHelp(object sender, ExecutedRoutedEventArgs e) {
            Utils.OpenHelp("Bulk.Automatic.Translation.html");
        }

        /// <summary>
        /// Sets the text on the status message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="showProgress"><c>true</c> if the progress bar should be shown, otherwise <c>false</c>.</param>
        private void SetStatus(string message, bool showProgress) {
            prgStatus.Visibility = showProgress ? Visibility.Visible : Visibility.Hidden;
            if(message == null) {
                lblStatus.Visibility = Visibility.Hidden;
            } else {
                lblStatus.Visibility = Visibility.Visible;
                lblStatus.Content = message;
            }
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, ExecutedRoutedEventArgs e) {
            Close();
        }
    }
}
